import router from '../../router';
import store from '@/store/index';

const tagsView = {
  state: {
    visitedViews: [],
    cachedViews: [],
    lastTagNo: 0, // 没创建一个tag，no加一
  },
  mutations: {
    ADD_VISITED_VIEWS: (state, view) => {
      // if (state.visitedViews.some(v => v.path === view.path)) {
      //   router.push({
      //     path: view.path,
      //     query: {
      //       tags: true,
      //       index: state.visitedViews.length
      //     }
      //   })
      // }
      const tagNo = Number(view.query.tagNo) || 0;
      state.lastTagNo = Number(state.lastTagNo < tagNo ? tagNo : state.lastTagNo);

      const newView = {
        path: view.path,
        name: view.name,
        title: view.meta.title || 'no-name',
        tagNo,
        query: view.query,
      };
      state.visitedViews.push(newView);
      if (!view.meta.noCache) {
        state.cachedViews.push(view.name);
      }
    },
    UPDATE_VISITED_VIEWS: (state, view) => {
      const {tagNo} = view.query;
      const findView = _.find(state.visitedViews, (item) => item.tagNo == tagNo);
      if (findView && findView.query) {
        findView.query = view.query;
      }

    },
    DEL_VISITED_VIEWS: (state, tagNo) => {

      // state.visitedViews.splice(index, 1);
      state.visitedViews = _.filter(state.visitedViews, (item) => item.tagNo != tagNo);
      // state.cachedViews.splice(index, 1)
      // for (const [i, v] of state.visitedViews.entries()) {
      //   if (v.path === view.path) {
      //     state.visitedViews.splice(i, 1)
      //     break
      //   }
      // }
      // for (const i of state.cachedViews) {
      //   if (i === view.name) {
      //     const index = state.cachedViews.indexOf(i)
      //     state.cachedViews.splice(index, 1)
      //     break
      //   }
      // }
    },
    DEL_OTHERS_VIEWS:
      (state, view) => {
        for (const [i, v] of state.visitedViews.entries()) {
          if (v.tagNo === view.tagNo) {
            state.visitedViews = state.visitedViews.slice(i, i + 1);
            break;
          }
        }
        for (const i of state.cachedViews) {
          if (i === view.name) {
            const index = state.cachedViews.indexOf(i);
            state.cachedViews = state.cachedViews.slice(index, i + 1);
            break;
          }
        }
      },
    DEL_ALL_VIEWS:
      (state) => {
        state.visitedViews = [];
        state.cachedViews = [];
      },
  },
  actions: {
    addVisitedViews({ commit }, view) {
      commit('ADD_VISITED_VIEWS', view);
    },
    updateVisitedViews({ commit }, view) {
      commit('UPDATE_VISITED_VIEWS', view);
    },
    delVisitedViews({ commit, state }, tagNo) {
      return new Promise((resolve) => {
        commit('DEL_VISITED_VIEWS', tagNo);
        resolve([...state.visitedViews]);
      });
    },
    delOthersViews({ commit, state }, view) {
      return new Promise((resolve) => {
        commit('DEL_OTHERS_VIEWS', view);
        resolve([...state.visitedViews]);
      });
    },
    delAllViews({ commit, state }) {
      return new Promise((resolve) => {
        commit('DEL_ALL_VIEWS');
        resolve([...state.visitedViews]);
      });
    },
  },
};

export default tagsView;
